package iconv;

enum Handle {}

class Converter {
	var handle: Handle;
	
	public function new(from: String, to: String) {
		handle = _open(neko.Lib.haxeToNeko(from), neko.Lib.haxeToNeko(to));
	}
	
	public function close(): Void {
		_close(handle);
	}
	
	public function convert(source: String): String {
		return neko.Lib.nekoToHaxe(_convert(handle, neko.Lib.haxeToNeko(source)));
	}
	
	public static function run(from: String, to: String, source: String): String {
		var converter = new Converter(from, to);
		var result = converter.convert(source);
		converter.close();
		return result;
	}
	
	static var _open = neko.Lib.load("libiconv", "open", 2);
	static var _close = neko.Lib.load("libiconv", "close", 1);
	static var _convert = neko.Lib.load("libiconv", "convert", 2);

}